/**
 * 比较各类互斥技术（二）
 *
 * @author fireway
 * @since 2019年 04月 04日 星期四 06:50:10 CST
 */
package currency;

public class SynchronizationComparisons {
    private static BaseLine sBaseLine = new BaseLine();

    private static SynchronizedTest sSynch = new SynchronizedTest();

    private static LockTest sLock = new LockTest();

    static AtomicTest sAtomic = new AtomicTest();

    private static void test() {
        System.out.println("============================");
        System.out.println(String.format("%-12s : %13d", "Cycles", Accumulator.sCycles));
        sBaseLine.timedTest();
        sSynch.timedTest();
        sLock.timedTest();
        sAtomic.timedTest();
        Accumulator.report(sSynch, sBaseLine);
        Accumulator.report(sLock, sBaseLine);
        Accumulator.report(sAtomic, sBaseLine);
        Accumulator.report(sSynch, sLock);
        Accumulator.report(sSynch, sAtomic);
        Accumulator.report(sLock, sAtomic);
    }

    public static void main(String[] args) {
        int iterations = 5;  // Default
        // Optionally change iterations
        if(args.length > 0) {
            iterations = new Integer(args[0]);
        }
        // The first time fills the thread pool
        System.out.println("Warmup");
        sBaseLine.timedTest();

        // Now the initial test doesn't include the cost
        // of starting the threads for the first time.
        // Produce multiple data points
        for(int i = 0; i < iterations; i++) {
            test();
            Accumulator.sCycles *= 2;
        }

        Accumulator.sExec.shutdown();
    }
}
